
/*Manifest file*/
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


/*Get and check the active network*/
final ConnectivityManager conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isAvailable() &&
 activeNetwork.getState() == NetworkInfo.State.CONNECTED) {
    //notify user you are online
} else {
    //notify user you are not online
} 


/*Check many network types*/
public void  checkNetworkStatus(){

    final ConnectivityManager connMgr = (ConnectivityManager)
     this.getSystemService(Context.CONNECTIVITY_SERVICE);

     final android.net.NetworkInfo wifi =
     connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

     final android.net.NetworkInfo mobile =
     connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

     if( wifi.isAvailable() ){

		Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
     }
     else if( mobile.isAvailable() ){

		Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
     }
     else
     {

         Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
     }

}

/*Checking throw a httpget*/
public static void isNetworkAvailable(final Handler handler, final int timeout) {

        // ask fo message '0' (not connected) or '1' (connected) on 'handler'
        // the answer must be send before before within the 'timeout' (in milliseconds)

        new Thread() {

            private boolean responded = false;

            @Override
            public void run() {

                // set 'responded' to TRUE if is able to connect with google mobile (responds fast)

                new Thread() {

                    @Override
                    public void run() {
                        HttpGet requestForTest = new HttpGet("http://m.google.com");
                        try {
                            new DefaultHttpClient().execute(requestForTest); // can last...
                            responded = true;
                        } catch (Exception e) {}
                    }

                }.start();

                try {
                    int waited = 0;
                    while(!responded && (waited < timeout)) {
                        sleep(100);
                        if(!responded ) { 
                            waited += 100;
                        }
                    }
                } 
                catch(InterruptedException e) {} // do nothing 
                finally { 
                    if (!responded) { handler.sendEmptyMessage(0); } 
                    else { handler.sendEmptyMessage(1); }
                }

            }

        }.start();

}


//
Handler h = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        if (msg.what != 1) { // code if not connected

        } else { // code if connected

        }

    }
};

//
//call
isNetworkAvailable(h,2000); // get the answser within 2000 ms

